package service

import (
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// sql.Db
// "database/sql
var Db *gorm.DB

func init() {
	var err error
	dsn := "root:123456@tcp(127.0.0.1:3306)/wms_beijing?charset=utf8mb4&parseTime=True&loc=Local"
	Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
}

// 首字母代表访问权限
type Product struct {
	Id          uint `gorm:"primaryKey;autoIncrement"`
	Name        string
	Code        string
	Create_date time.Time
	Description string
}

// 值方法
func (Product) TableName() string {
	return "eam_product"
}

// 反射，修改这个值，需要传递指针
func Insert(product *Product) {
	Db.Create(product)
}

func Query(name string) []Product {
	products := make([]Product, 1)
	Db.Where("name", name).Find(&products)
	return products
}
